'use strict';

const Controller = require('./base');

class BorrowController extends Controller {
    async page() {
        const { ctx } = this
        const query = ctx.query
        ctx.validate({
            page: { type: 'string', require: true },
            limit: { type: 'string', require: true }
        }, query)
        this.success(await ctx.service.borrow.page(query))
    }
    async borrowBook() {
        const { ctx } = this
        const body = ctx.request.body
        ctx.validate({
            // username: { type: 'string', required: true },
            book_num: { type: 'string', required: true },
            borrow_time: { type: 'integer', required: true },
        }, body);
        const res = await ctx.service.borrow.borrowBook(body)
        if (res) {
            if(res.msg) {
                this.error(res.msg)
            } else {
                this.success('借阅成功!')
            }
        } else {
            this.error('借阅失败!')
        }
    }
    async revertBook() {
        const { ctx } = this
        const body = ctx.request.body
        ctx.validate({
            book_num: { type: 'string', required: true },
        }, body);
        const res = await ctx.service.borrow.revertBook(body)
        if (res) {
            this.success('归还成功!')
        } else {
            this.error('归还失败!')
        }
    }
}

module.exports = BorrowController;
